﻿
using System;

namespace ClientAPI
{
    public class Model
    {
        private static Model _clientAPIInstance = null;

        public delegate void ChatEventHandler(object sender, UpdateChatEventArgs eargs);
        public delegate void ChatListEventHandler(object sender, UpdateChatListEventArgs eargs);
        public delegate void CurrentChatsEventHandler(object sender, UpdateCurrentChatsEventArgs eargs);

        public event ChatEventHandler _updateChatHandler;
        public event ChatListEventHandler _chatListHandler;
        public event CurrentChatsEventHandler _currentChatsHandler;

        private NetworkController _network;
        private CommandProtocol _command = CommandProtocol.Instance;
        private Settings _settings = Settings.Instance;
        private ChatProtocol _chat = new ChatProtocol();
        private ProjectController _project;

        bool _loggedIn = false;

        public static Model Instance
        {
            get
            {
                if (_clientAPIInstance == null)
                    _clientAPIInstance = new Model();
                return _clientAPIInstance;
            }
        }

        public Model()
        {
            _settings.ImportConfigurations();
            _network = new NetworkController();
            _project = new ProjectController();
            _command.SetProjectsController(_project);
            _command.SetNetworkController(_network);
        }

        public void Connect()
        {
            _network.Connect();
        }

        public bool IsConnected()
        {
            return _network.IsConnected();
        }

        public bool Login(string user, string pass)
        {
            _command.Login(user, pass);
            _loggedIn = _command.WaitForLoginReply();
            if (_loggedIn) _network.ChatConnect(_chat);
            return _loggedIn;
        }

        public bool LogOut()
        {
            _command.Disconnect();
            return false;
        }

        public void CreateUser(string user, string first, string last, string pass, string phone, string email)
        {
            _command.CreateUser(user, first, last, pass, phone, email);
        }

        public void SendFile(string location, string name, string version, string thanks, string info)
        {
            string description = name + "µ" + _command.GetUserName() + "µ" + DateTime.Now.Date.ToShortDateString() + "µ" + version + "µ" + thanks + "µ" + info + "µ";
            _network.SendFileConnect(location, description);
        }

        public void Download(string fileName)
        {
            _network.ReceiveFileConnect(fileName);
        }

        public void UpdateChat()
        {
            if (_updateChatHandler != null)
                _updateChatHandler(this, new UpdateChatEventArgs());
        }

        public void UpdateChatList()
        {
            if (_chatListHandler != null)
                _chatListHandler(this, new UpdateChatListEventArgs());
        }

        public void UpdateCurrentChats()
        {
            if (_currentChatsHandler != null)
                _currentChatsHandler(this, new UpdateCurrentChatsEventArgs());
        }

        public void SendChatMessage(string messageReceiver, string chatMessage)
        {
            _chat.SendChatMessage(messageReceiver, chatMessage);
            UpdateChat();
        }

        public void ChatMessageReceived()
        {
            UpdateCurrentChats();
            UpdateChat();
        }

        public string GetChatLog(string userName)
        {
            return _chat.GetLog(userName);
        }

        public string[] GetChatList()
        {
            return _chat.GetChatList();
        }

        public string[] GetCurrentChats()
        {
            return _chat.GetCurrentChats();
        }

        public void SetCurrentTab(string currentTab)
        {
            UpdateChat();
        }

        public void CloseChat(string user)
        {
            _chat.CloseLog(user);
            UpdateChat();
            UpdateChatList();
        }

        public string[] GetProjectsList()
        {
            return _project.GetProjectsArray();
        }

        public string[] GetProjectInfo(string key)
        {
            return _project.GetProjectValues(key);
        }

        public string[] GetProjectSearch(string value)
        {
            return _project.GetProjectSearch(value);
        }
    }
}

public class UpdateChatEventArgs
{
    private string _chatLog;

    public string chatLog
    {
        get
        {
            return _chatLog;
        }

        set
        {
            _chatLog = value;
        }
    }
}

public class UpdateChatListEventArgs
{
    private string[] _chatList;

    public string[] chatList
    {
        get
        {
            return _chatList;
        }

        set
        {
            _chatList = value;
        }
    }
}

public class UpdateCurrentChatsEventArgs
{
    private string[] _currentChatList;

    public string[] chatList
    {
        get
        {
            return _currentChatList;
        }

        set
        {
            _currentChatList = value;
        }
    }
}


